package com.mashibing.test.case6;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.core.PriorityOrdered;

public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		System.out.println("----------MyBeanDefinitionRegistryPostProcessor postProcessBeanDefinitionRegistry----------");
		System.out.println("【1】创建Teacher的BeanDefinition并设置属性值为zhangsan");
		BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Teacher.class);
        builder.addPropertyValue("name","zhangsan");
        registry.registerBeanDefinition("teacher",builder.getBeanDefinition());
        System.out.println("【2】创建MySelfBeanDefinitionRegistryPostProcessor的BeanDefinition,测试重复查找【执行过程中新增其他的BeanDefinitionRegistryPostProcessor】");  
        BeanDefinitionBuilder builder1 = BeanDefinitionBuilder.rootBeanDefinition(MySelfBeanDefinitionRegistryPostProcessor.class);
        registry.registerBeanDefinition("msb",builder1.getBeanDefinition());
	}

	
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("----------MyBeanDefinitionRegistryPostProcessor postProcessBeanFactory----------");
        BeanDefinition bd = beanFactory.getBeanDefinition("teacher");
        System.out.println("【3】获取Teacher的BeanDefinition并设置转换后属性值为lisi");
        bd.getPropertyValues().getPropertyValue("name").setConvertedValue("lisi");
        System.out.println("===============");
	}

	@Override
	public int getOrder() {
		return 0;
	}
}
